﻿/******************************************************************************
    Copyright (C) 2020 by Zaodao(Dalian) Education Technology Co., Ltd.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#ifndef ZDTALK_RECORDER_H_
#define ZDTALK_RECORDER_H_

#include <QObject>
#include <QString>
#include <QCoreApplication>

#include "recorder-client.h"
#include "util/profiler.hpp"
#include "util/util.hpp"
#include "obs.hpp"
#include "../obs-context-wrapper.h"

// 录制配置结构体
struct RecorderConfig {
    QString logPath;              // 日志文件路径
    QString crashPath;            // 崩溃日志路径
    QString userName;             // 用户名
    QString userId;               // 用户ID
    QString serverName;           // Socket服务器名称
    int recordingSourceType;      // 录制源类型 (0=device, 1=shared-memory)
    QString sharedMemoryName;     // 共享内存名称
    
    RecorderConfig() : recordingSourceType(0) {}
};

class ZDTalkRecorder : public QObject
{
    Q_OBJECT

public:
    ZDTalkRecorder(QObject *parent = nullptr);
    ~ZDTalkRecorder();

    config_t *GetGlobalConfig() const { return global_config_; }
    const char *InputAudioSource() const;
    const char *OutputAudioSource() const;

    int Init(const QString &server, const QString &config);
    
    // 设置录制配置
    void SetConfig(const RecorderConfig &config) { m_config = config; }
    const RecorderConfig &GetConfig() const { return m_config; }

    profiler_name_store_t *GetProfilerNameStore() const
    {
        return profilerNameStore;
    }
    
    // 静态访问方法，替代App()函数
    static ZDTalkRecorder* GetInstance() { return s_instance; }
    
    // 获取RecorderClient实例（用于信号连接）
    RecorderClient* getRecorderClient() { return client_; }

private:
    QString GetValidBasePath();
    bool InitGlobalConfig(const QString &path);
    void InitDefaultsGlobalConfig();
    void ApplyConfig();  // 应用配置

private:
    ConfigFile global_config_;
    std::unique_ptr<OBSContextWrapper> obs_context_;
    RecorderClient *client_ = nullptr;
    RecorderConfig m_config;  // 录制配置

    profiler_name_store_t *profilerNameStore = nullptr;
    
    // 静态实例指针
    static ZDTalkRecorder* s_instance;
};

// 提供App()函数的替代品，保持向后兼容
inline ZDTalkRecorder *App()
{
    return ZDTalkRecorder::GetInstance();
}

#endif  // ZDTALK_RECORDER_H_
